package com.jason.campusinn.view;

import android.app.Activity;
import android.content.Context;
import android.content.res.AssetManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.yuandian.wanna.R;
import com.yuandian.wanna.activity.navigationDrawer.OrderMeasureActivity;
import com.yuandian.wanna.utils.CommonMethodUtils;
import com.yuandian.wanna.wheelview.ArrayWheelAdapter;
import com.yuandian.wanna.wheelview.OnWheelChangedListener;
import com.yuandian.wanna.wheelview.WheelView;
import com.yuandian.wanna.wheelview.XmlNormalParserHandler;

import java.io.InputStream;
import java.util.List;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

/**
 * Created by yuandian on 2015/5/21.
 */
public class PopupWindow extends android.widget.PopupWindow implements  OnWheelChangedListener {
    private View view;
    private Activity context;
    private WheelView mViewFirst;
    private String value;
    /**
     * 所有省
     */
    protected String[] mFirstDatas;


    public PopupWindow(Activity context, int viewLenth, String parseFileName) {

        this.context = context;

        //初始化页面
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.popup_choose, null);
        setContentView(view);
        mViewFirst = (WheelView) view.findViewById(R.id.first_selection);
        // 添加change事件
        mViewFirst.addChangingListener(this);
        setUpData(viewLenth, parseFileName);

        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        this.setFocusable(true);
        this.setAnimationStyle(R.style.popupStyle);
        this.setOutsideTouchable(true);
        this.update();

        setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                showSelectedResult();
            }
        });
    }

    @Override
    public void onChanged(WheelView wheel, int oldValue, int newValue) {
    }

    private void setUpData(int viewLenth, String parseFileName) {
        initProvinceDatas(parseFileName);
        mViewFirst.setViewAdapter(new ArrayWheelAdapter<String>(context, mFirstDatas));
        // 设置可见条目数量
        mViewFirst.setVisibleItems(viewLenth);
    }

    /**
     * 初始化控件地区信息
     */
    protected void initProvinceDatas(String parseFileName) {
        List<String> provinceList = null;
        AssetManager asset = context.getAssets();
        try {
            InputStream input = asset.open(parseFileName);
            // 创建一个解析xml的工厂对象
            SAXParserFactory spf = SAXParserFactory.newInstance();
            // 解析xml
            SAXParser parser = spf.newSAXParser();
            XmlNormalParserHandler handler = new XmlNormalParserHandler();
            parser.parse(input, handler);
            input.close();
            // 获取解析出来的数据
            provinceList = handler.getValues();
            mFirstDatas = new String[provinceList.size()];
            for (int i=0; i<provinceList.size(); i++)
            {
                mFirstDatas[i] = provinceList.get(i);
            }
        } catch (Throwable e) {
            e.printStackTrace();
        } finally {

        }
    }

    public void setValue(String value)
    {
        if(CommonMethodUtils.isEmpty(value))
            return;
        this.value = value;
        if(mFirstDatas.length != 0)
        {
            for (int i=0; i<mFirstDatas.length; i++)
            {
                if(value.equals(mFirstDatas[i]))
                {
                    mViewFirst.setCurrentItem(i);
                }
            }
        }
    }

    private void showSelectedResult() {
        callback.setValue(mFirstDatas[mViewFirst.getCurrentItem()]);
    }

    private Callback callback;

    public Callback getCallback() {
        return callback;
    }


    public void setCallback(Callback callback) {
        this.callback = callback;
    }

    public interface Callback
    {
        void setValue(String value);
    }

}
